
#include "Thread_Table.h"
#include "Message.h"
#include <iostream>

Thread_Table::Thread_Table(int number_of_threads)
		: _number_of_threads(number_of_threads){
	_table = new vector<EntryTT*>();
	_table->push_back(NULL);
	for (int i = 1; i < _number_of_threads+1; ++i) {
		_table->push_back(new EntryTT(i));
	}

}

Thread_Table::~Thread_Table() {
	delete(_table);
}

EntryTT* Thread_Table::get_worker_entry(int index)
{
	if ( (int)_table->size() > index )
	{
		return _table->at(index);
	}
	else
	{
		return NULL;
	}
}

void Thread_Table::print()
{
	if ( Message::print_allowed )
	{
		cout << endl <<"Threads Table:" << endl;
		cout <<        "--------------" << endl;
		cout << "Worker   Status   Accu_Msg_Qua.   Delivered_from_last   Priority   Quanta_left_last_time" << endl;
		for (int i = 1; i < _number_of_threads+1; ++i) {
			_table->at(i)->print();
		}
		cout << endl;
	}
}

void Thread_Table::block_all()
{
	for (int i = 1; i < _number_of_threads+1; ++i) {
		((EntryTT*)_table->at(i))->setStatus(THREAD_BLOCKED);
	}
}

void Thread_Table::re_init_TT()
{
	for (int i = 1; i < _number_of_threads+1; ++i) {
		EntryTT* e = get_worker_entry(i);
		e->setStatus(THREAD_READY);
		e->setAcumulated_message_quanta(0);
		e->setTotal_number_delivered_from_last_quantum(0);
		e->setPriority(0);
		e->setCurrent_quanta(0);
	}
}
